package com.example.demo03_mvm.model;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.demo03_mvm.db.Goods;
import com.example.demo03_mvm.http.ApiService;
import com.example.demo03_mvm.http.RetrofitUtils;

import java.util.List;

import io.reactivex.Completable;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class BaseModel {

    public ApiService getApi(){
        return RetrofitUtils.getRetrofitUtils().initRetrofit();
    }


    public <T>MutableLiveData<T> getData(Observable observable,MutableLiveData<T> liveData){
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Object o) throws Exception {
                        liveData.postValue((T)o);
                    }
                });
        return liveData;
    }

    CompositeDisposable compos=new CompositeDisposable();
    //Room增删改
    public void update(Completable com, Action action){
        compos.add(com.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(action));

    }

    public void select(Flowable flowable, Consumer consumer){
        compos.add(flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(consumer));
    }
}
